import {
  createRouter,
  createWebHashHistory,
} from "vue-router";
import NProgress from 'nprogress'

const routes = [
  {
    path: '/',
    name: '登录',
    component: () => import('@/views/login'),
  },
  {
    path: '/registered',
    name: '注册',
    component: () => import('@/views/registered'),
  },
  {
    path: '/forgetPass',
    name: '忘记密码',
    component: () => import('@/views/forgetPass'),
  },
  {
    path: '/changePass',
    name: '修改密码',
    component: () => import('@/views/changePass'),
  },
  {
    path: '/home',
    name: '主页',
    component: () => import('@/views/home'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: '首页',
        component: () => import('@/views/welcome'),
      },
      {
        path: '/myStore',
        name: '员工管理',
        redirect: '/employeesMng',
        children: [
          {
            path: '/employeesMng',
            name: '员工列表',
            component: () => import('@/views/employeesMng'),
            children: []
          },
          {
            path: '/jobsMng',
            name: '角色管理',
            component: () => import('@/views/jobsMng'),
            children: []
          },
        ]
      },
      {
        path: '/offlineReplacement',
        name: '线下代充',
        component: () => import('@/views/offlineReplacement'),
        children: [
          {
            path: '/offlineReplacement/detail',
            name: '代充详情',
            component: () => import('@/views/offlineReplacement/detail'),
            children: []
          },
        ]
      },
      {
        path: '/commercial',
        name: '商户信息',
        component: () => import('@/views/commercial'),
        children: [
          {
            path: '/commercial/detail',
            name: '商户详情',
            component: () => import('@/views/commercial/detail'),
            children: []
          },
        ]
      },
      {
        path: '/yysApi',
        name: '运营商生产接口',
        redirect: '/autoProduction',
        children: [
          {
            path: '/autoProduction',
            name: '自动生产对接',
            component: () => import('@/views/autoProduction'),
            children: []
          },
          {
            path: '/setmealMng',
            name: '上游平台',
            component: () => import('@/views/setmealMng'),
            children: []
          },
        ]
      },
      {
        path: '/downApi',
        name: '下游接口管理',
        component: () => import('@/views/downApi'),
      },
      {
        path: '/dataStatistics',
        name: '数据统计',
        redirect: '/orderStatistics',
        children: [
          {
            path: '/orderStatistics',
            name: '订单统计',
            component: () => import('@/views/orderStatistics'),
            children: []
          },
          {
            path: '/accountStatistics',
            name: '账户统计',
            component: () => import('@/views/accountStatistics'),
            children: []
          },
          {
            path: '/commercialStatistics',
            name: '商户统计',
            component: () => import('@/views/commercialStatistics'),
            children: []
          },
          {
            path: '/productStatistics',
            name: '产品统计',
            component: () => import('@/views/productStatistics'),
            children: []
          },
        ]
      },
      {
        path: '/sysMsg',
        name: '系统消息',
        component: () => import('@/views/sysMsg'),
      },
      {
        path: '/softwareRelease',
        name: '版本管理',
        component: () => import("@/views/softwareRelease"),
        redirect: '/softwareRelease/projectManagement',
        children: [{
          path: '/softwareRelease/projectManagement',
          name: '项目管理',
          component: () => import("@/views/softwareRelease/projectManagement"),
          meta: {
            openName: "/softwareRelease",
          },
        },
        {
          path: '/softwareRelease/products',
          name: '产品列表',
          component: () => import("@/views/softwareRelease/products"),
          meta: {
            openName: "/softwareRelease",
          },
        },
        {
          path: '/softwareRelease/softwareManagement',
          name: '软件管理',
          component: () => import("@/views/softwareRelease/softwareManagement"),
          meta: {
            openName: "/softwareRelease",
          },
        },
        ]
      },
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

const wtiterPath = ['/', '/registered', '/forgetPass'];
router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = window.localStorage.getItem('JHH_USER_TOKEN')
  if (!token && !wtiterPath.includes(to.path)) {
    next({ path: "/" })
    return
  }

  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
